package com.project.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.project.common.BaseContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * ClassName: MyMetaObjectHandler
 * Package: com.project.handler
 * Description:    公共字段填充
 *
 * @Author 看到风
 * @Create 2023/10/21 16:27
 * @Version 1.0
 */
@Component
@Slf4j
public class MyMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("公共字段自动填充【insert】。。。");
        log.info(metaObject.toString());
        metaObject.setValue("createTime", LocalDateTime.now());
        metaObject.setValue("modifiedTime", LocalDateTime.now());
        metaObject.setValue("createUserId", BaseContext.getCurrentId());
        metaObject.setValue("modifiedUserId", BaseContext.getCurrentId());
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("公共字段自动填充【update】。。。");
        log.info(metaObject.toString());
        metaObject.setValue("modifiedTime",LocalDateTime.now());
        metaObject.setValue("modifiedUserId",BaseContext.getCurrentId());
    }
}
